// pages/mine/a.js
import routerManager from "../../../utils/routerManager.js";
import http from "../../../utils/http.js";

const app = getApp();

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    hasUserInfo: {
      // 属性名
      type: Boolean, // 类型（必填），目前接受的类型包括：String, Number, Boolean, Object, Array, null（表示任意类型）
      value: false // 属性初始值（可选），如果未指定则会根据类型选择一个
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    userInfo: {},
    finished_orders_count: 0,
    refunded_orders_count: 0,
    waiting_orders_count: 0,
    wallet_balance: 0
  },
  ready() {
    this.loadData();
  },
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function() {},
    moved: function() {},
    detached: function() {}
  },
  /**
   * 组件的方法列表
   */
  methods: {
    loadData() {
      let that = this;
      wx.getSetting({
        success(res) {
          let hasUserInfo = res.authSetting["scope.userInfo"];
          if (hasUserInfo) {
            app.globalData.userInfo = wx.getStorageSync("user_info");
            app.globalData.token = wx.getStorageSync("token");
            that.setData({
              userInfo: app.globalData.userInfo,
              hasUserInfo: true
            });
            http.customerMy(res => {
              if (res.error == 0) {
                that.setData({
                  finished_orders_count: res.data.finished_orders_count,
                  refunded_orders_count: res.data.refunded_orders_count,
                  waiting_orders_count: res.data.waiting_orders_count,
                  wallet_balance: res.data.wallet_balance
                });
              }
            });
            // http.customerConfigs(res => {
            //   console.log("res", res);
            // });
          }
        }
      });
    },
    toOrders: function() {
      routerManager.toOrders();
    },
    toMoney: function() {
      routerManager.toMoney();
    },
    userInfoCallback: function(event) {
      let that = this;
      http.oauthLogin(event, data => {
        that.setData({
          hasUserInfo: true,
          userInfo: data.data
        });
        that.loadData();
        app.initTimSDK();
      });
    },
    toQuestions: function(event) {
      routerManager.toQuestions();
    }
  }
});
